const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//request, 利用 Promise 封装
const newhttp = (url, method = 'GET', data = {}) => {
  let p = new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      success: function (res) {
        resolve(res)
      },
      fail: function (error) {
        reject(res)
      },
      complete: function (res) {

      }
    })
  })
  return p
}

const openToast = (title, icon) => {
  wx.showToast({
    title: title,
    mask: true,
    icon: icon,
    duration: 1000
  });
}

const openErrorToast = (title) => {
  wx.showToast({
    title: title,
    mask: true,
    image: '/images/icon/close.png',
    duration: 1500
  });
}

//获取js今天的日期 '2019-10-17'
const getTodayDate = (n = 0) => {
  let month = '';
  let date = new Date()
  date.setTime(date.getTime() + 24 * 60 * 60 * 1000 * n)
  let year = date.getFullYear()

  if ((date.getMonth() + 1) < 10) {
    month = '0' + (date.getMonth() + 1);
  } else {
    month = (date.getMonth() + 1);
  }

  let day = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()

  return year + '-' + month + '-' + day;
}

//通过"2019-10-20" 转为 "2019-10-20 星期日 今天"
const processDateNum = date => {
  //处理时间,显示今天,明天
  //获取现在时间
  //转为时间戳
  let n = (new Date(date)).getTime()
  //获取今天时间戳
  let today = (new Date(getTodayDate())).getTime()
  //1天的时间
  let num = 24 * 60 * 60 * 1000
  let ret = Math.floor((n - today) / num)
  switch (ret) {
    case 0:
      return date + ' ' + getWeekDay(date) + ' 今天'
      break;
    case 1:
      return date + ' ' + getWeekDay(date) + ' 明天'
      break;
    default:
      return date
      break;
  }
}

//通过'2018-08-08'的方式获取周几
const getWeekDay = date => {
  let time = new Date(date)
  let weekday = time.getDay()
  let dateStr = '星期' + '日一二三四五六'.charAt(weekday)
  return dateStr
}

//将2个汉子转为3个汉子的样式, 例如'张强' 转为 '张 强'
const processTwoToThreeStr = str => {
  if (str.length === 2) {
      str = str.slice(0, 1) + ' ' + str.slice(1)
  }
  return str;
}

//去掉数组空值
const trimSpace = array => {
  for (var i = 0; i < array.length; i++) {
      if (array[i] == " " || array[i] == null || typeof (array[i]) == "undefined") {
          array.splice(i, 1);
          i = i - 1;
      }
  }
  return array;
}

module.exports = {
  formatTime: formatTime,
  openToast: openToast,
  getTodayDate: getTodayDate,
  processDateNum: processDateNum,
  getWeekDay: getWeekDay,
  openErrorToast: openErrorToast,
  processTwoToThreeStr: processTwoToThreeStr,
  trimSpace:trimSpace,
  newhttp: newhttp
}